<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Change Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	
	function password($id=FALSE,$link= FALSE)
	{
		if(is_numeric($id) AND $this->authlink->verify_authlink($link))
		{
			$this->load->model('M_users');
			$old_pass 	= $this->input->post('old_password');
			$new_pass 	= $this->input->post('new_password');
			$c_new_pass = $this->input->post('c_new_password');
			$user_id = $this->input->post('id');
			$this->view_data['id'] = $id;
			$this->view_data['auth'] = $link;
			
			if($this->form_validation->run('user_change_pass') == FALSE)
			{
				$this->view_data['system_message'] = validation_errors() == '' ? NULL : $this->_msg('e',validation_errors());
			}else
			{	
				if($this->M_users->verify_password($old_pass,$id)== TRUE){
					if($this->M_users->change_pass($new_pass,$id) == TRUE)
					{
						$this->authlink->clear_authlink();
						$this->_msg('p','Successfully changed your password. Please login with new credentials.','auth/logout');
					}else{
						$this->view_data['system_message'] = $this->_msg('e','Unable to update password.');
					}
				}else{
					$this->view_data['system_message'] = $this->_msg('e','Wrong password entered.');
				}
			}
		}else{
			show_404();
		}
	}
	
	public function cancel()
	{
		$this->authlink->clear_authlink();
		$this->_msg('n','Canceled change password.',$this->session_user);
	}

	/**
		* Change Current School Year
		*@param string hash User ID
		*/
	public function school_year($id, $link = false)
	{

		$this->view_data['use_bootstrap3'] = true; // Use the Bootrap V3 and JS
		$this->view_data['system_message'] = $this->_msg();
		$this->load->helper('panel');
		$this->load->model('M_school_years');
		$this->load->model('M_user_open_sy');

		if(is_numeric($id) AND $this->authlink->verify_authlink($link))
		{

			$this->view_data['current'] = $ct = $this->M_user_open_sy->get_user_profile($id);

			unset($get);
			if($ct){
				$get['where']['id <> '] = $ct->id;
			}
			$get['order'] = "sy_from";

			$this->view_data['school_years'] = $this->M_user_open_sy->get_record('school_years', $get);

			if($this->input->post('change_sy')){

				if($this->input->post('sy')){

					unset($data);
					$data['sy_id'] = $this->input->post('sy');
					$xrs_upd = $this->M_user_open_sy->update($ct->user_open_sy_id, $data);
				
					if($xrs_upd){
						$this->_msg('s','Your current school year was successfully changed.', current_url());
					}else{
						$this->_msg('e','School Year failed to be updated', current_url());
					}

				}else{

					$this->_msg('e','No Selected School Year', current_url());

				}
			}

		}else{
			show_404();
		}
	}

	/*
	 * Set School Year
	 * Set School Year directly from the top nav menu
	 */
	public function set_school_year($sy_id = false, $link = false)
	{
		if(is_numeric($sy_id) AND $this->authlink->verify_authlink($link))
		{
			$this->load->model('M_user_open_sy');
			$rs = $this->M_user_open_sy->set_user_school_year($sy_id);
			// vd($rs);
			if($rs){
				$this->_msg('s','School Year was successfully set.', $this->session->userdata('refered_from'));
			}else{
				$this->_msg('e','School Year was not successfully set. Please try again', $this->session->userdata('refered_from'));
			}
		}else{
			show_404();
		}
	}
	
	/*
	 * Set tool tip from the top nav menu
	*/
	public function set_tool_tip($link = false)
	{
		if($this->authlink->verify_authlink($link))
		{
			$this->load->model('M_user_tool_tip');
			$rs = $this->M_user_tool_tip->set_user_tool_tip();
			
			if($rs){
				$this->_msg('s','Successfully tooltip toogled.', $this->session->userdata('refered_from'));
			}else{
				$this->_msg('e','Tooltip toogle was not successfully. Please try again', $this->session->userdata('refered_from'));
			}
		}else{
			show_404();
		}
	}
}